$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'storeMaterialCheck/storematerialcheck/list',
        datatype: "json",
        colModel: [			
			{ label: 'checkId', name: 'checkId', index: 'check_id', width: 50, key: true },
			{ label: '单据号', name: 'checkNo', index: 'check_no', width: 80 }, 			
			{ label: '仓库id', name: 'stockId', index: 'stock_id', width: 80 }, 			
			{ label: '单据状态', name: 'status', index: 'status', width: 80,
                formatter:function(value,options,row){
                    if(value == 1){
                        return '待提交';
                    }else if(value == 2){
                        return '待审核';
                    }else if(value == 3){
                        return '已审核';
					}
                }
			},
			{ label: '备注', name: 'remark', index: 'remark', width: 80 }, 			
			{ label: '审核人', name: 'checkName', index: 'check_name', width: 80 }, 			
			{ label: '审核时间', name: 'checkTime', index: 'check_time', width: 80 }, 			
			{ label: '审核备注', name: 'checkRemark', index: 'check_remark', width: 80 }, 			
			{ label: '创建人', name: 'createName', index: 'create_name', width: 80 }, 			
			{ label: '创建时间', name: 'createTime', index: 'create_time', width: 80 }			
        ],
		viewrecords: true,
        height: 650,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		storeMaterialCheck: {},
        storeMaterialCheckDetailVoList:[],
		updateFlag:false,
        e:{
            checkStatus:[
                {label:'待审核',value:1},
                {label:'审核通过',value:2},
                {label:'驳回',value:3}
            ]
        },
        mark:{
			statusMark:false
		}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			this.mark.statusMark = false;
            this.updateFlag = false;
			vm.showList = false;
			vm.title = "新增";
			vm.storeMaterialCheck = {};
			//获取该门店所有耗材列表
			$.ajax({
                type: "POST",
                url: baseURL + "storeMaterialCheck/storematerialcheck/getStoreStockMaterial",
                contentType: "application/json",
                success: function(r){
                    if(r.code === 0){
                        vm.storeMaterialCheckDetailVoList = r.storeMaterialCheckDetailVoList;
                    }else{
                        alert(r.msg);
                    }
                }
			})
		},
		update: function (event) {
			var checkId = getSelectedRow();
			if(checkId == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(checkId)
		},
		saveOrUpdate: function (event) {
			var url = vm.storeMaterialCheck.checkId == null ? "storeMaterialCheck/storematerialcheck/save" : "storeMaterialCheck/storematerialcheck/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.storeMaterialCheck),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var checkIds = getSelectedRows();
			if(checkIds == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "storeMaterialCheck/storematerialcheck/delete",
                    contentType: "application/json",
				    data: JSON.stringify(checkIds),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(checkId){
			$.get(baseURL + "storeMaterialCheck/storematerialcheck/info/"+checkId, function(r){
                vm.storeMaterialCheck = r.storeMaterialCheck;
            });
		},

		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		},

        edit:function(){
            this.mark.statusMark = true;
            var checkId = getSelectedRow();
            if(checkId == null){
                return ;
            }
            this.updateFlag = true;
            var self = this;
            $.get(baseURL + "storeMaterialCheck/storematerialcheck/checkInfo/"+checkId, function(r){
            	self.showList = false;
                self.storeMaterialCheckDetailVoList = r.storeMaterialCheckDetailVoList;
            });
		},
        //盘点计算
        calculateMaterial:function(index){
			var storeMaterialCheckDetailVo = this.storeMaterialCheckDetailVoList[index];
            storeMaterialCheckDetailVo.checkResult  = storeMaterialCheckDetailVo.actualQuantity - storeMaterialCheckDetailVo.systemQuantity
		},
		//返回
        goBack:function(){
            this.showList = true;
            this.storeMaterialCheckDetailVoList = [];
		},
        formatterStatus:function(row, column, cellValue, index){
			var self = this;
			debugger;
			var str = "";
			$.each(self.e.checkStatus,function(i,val){
                if(val.value == row.status){
                    str =  val.label;
				}
			});
			return str;
		},

		//盘点保存
        checkSave:function(){
			if(this.validate()){
				return ;
			}
			var self =  this;
			$.ajax({
                type: "POST",
                url: baseURL + "storeMaterialCheck/storematerialcheck/checkSave/"+self.updateFlag,
                contentType: "application/json",
                data: JSON.stringify(self.storeMaterialCheckDetailVoList),
                beforeSend: function () {
                    $("#saveBtn").attr({disabled:"disabled"});
                    console.info("按钮已经被禁用")
                },
                complete:function(){
                    $("#saveBtn").removeAttr("disabled");
                    console.info("按钮已经被恢复")
                },
                success: function(r){
                    if(r.code == 0){
                        alert('操作成功', function(index){
							self.showList = true;
							self.storeMaterialCheckDetailVoList = [];
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
			})
		},
		//校验
		validate:function(){
            for (var storeMaterialCheckDetailVo of this.storeMaterialCheckDetailVoList) {
				if(isBlank(storeMaterialCheckDetailVo.checkResult) && storeMaterialCheckDetailVo.checkResult != 0){
					alert("耗材:"+storeMaterialCheckDetailVo.materialName+"未成功计算盘点结果!")
					return true;
				}
            }
			return false;
		}

	}
});